﻿using Dapper;
using System;
using BCEntity.OA.WorkflowActive.WorkflowActivityStepFormConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowActive.WorkflowActivityStepFormConfig
{
	/// <summary>
	/// 工作流活动步骤表单配置
	/// </summary>
	public class WorkflowActivityStepFormConfigData : IWorkflowActivityStepFormConfigData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowactivitystepformconfig";
		private const string QUERY = @"SELECT WorkflowActivityStepFormConfigId,FormCategoryName,FormConfigTitle,FormConfigCode,IsSpecificForm,SpecificFormName,CreateAdminId,
											CreateAdminName,CreateTime,EditAdminId,EditAdminName,WorkflowActivityId,UpdateTime,TemplateCode,TemplateName,StepCode,WorkflowCode,
											WorkflowName,Step,FormCategoryCode,SubmitRole
										FROM oa_workflowactivitystepformconfig";
		public WorkflowActivityStepFormConfigData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流活动步骤表单配置是否存在
		public async Task<bool> Exists(long workflowActivityStepFormConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowActivityStepFormConfigId=?WorkflowActivityStepFormConfigId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepFormConfigId", workflowActivityStepFormConfigId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流活动步骤表单配置
		public async Task<WorkflowActivityStepFormConfigEntity> Add(WorkflowActivityStepFormConfigEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowactivitystepformconfig", new string[] { "FormCategoryName", "FormConfigTitle", "FormConfigCode", "IsSpecificForm", 
				"CreateAdminId", "CreateAdminName", "WorkflowActivityId", "TemplateCode", "TemplateName", "StepCode", "WorkflowCode", "WorkflowName", "Step", "FormCategoryCode","SubmitRole" });
			sql = string.Format("{0} {1} WHERE WorkflowActivityStepFormConfigId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormCategoryName", entity.FormCategoryName?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?IsSpecificForm", entity.IsSpecificForm, DbType.Boolean);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?WorkflowActivityId", entity.WorkflowActivityId, DbType.Int64, size: 20);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?StepCode", entity.StepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?WorkflowCode", entity.WorkflowCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?WorkflowName", entity.WorkflowName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?Step", entity.Step, DbType.Int32, size: 11);
			parameters.Add("?FormCategoryCode", entity.FormCategoryCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?SubmitRole", entity.SubmitRole?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowActivityStepFormConfigEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作流活动步骤表单配置
		public async Task<WorkflowActivityStepFormConfigEntity> Update(WorkflowActivityStepFormConfigEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowactivitystepformconfig", new string[] { "FormCategoryName", "FormConfigTitle", "FormConfigCode", "IsSpecificForm", 
				"SpecificFormName", "EditAdminId", "EditAdminName", "WorkflowActivityId", "TemplateCode", "TemplateName", "StepCode", "WorkflowCode", "WorkflowName", "Step", "FormCategoryCode","SubmitRole" }
				, new string[] { "WorkflowActivityStepFormConfigId" });
			sql = string.Format("{0} {1} WHERE WorkflowActivityStepFormConfigId=?WorkflowActivityStepFormConfigId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepFormConfigId", entity.WorkflowActivityStepFormConfigId, DbType.Int64, size: 20);
			parameters.Add("?FormCategoryName", entity.FormCategoryName?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?IsSpecificForm", entity.IsSpecificForm, DbType.Boolean);
			parameters.Add("?SpecificFormName", entity.SpecificFormName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?WorkflowActivityId", entity.WorkflowActivityId, DbType.Int64, size: 20);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?StepCode", entity.StepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?WorkflowCode", entity.WorkflowCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?WorkflowName", entity.WorkflowName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?Step", entity.Step, DbType.Int32, size: 11);
			parameters.Add("?FormCategoryCode", entity.FormCategoryCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?SubmitRole", entity.SubmitRole?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowActivityStepFormConfigEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流活动步骤表单配置
		public async Task<bool> Delete(long workflowActivityStepFormConfigId)
		{
			string sql = @"DELETE FROM oa_workflowactivitystepformconfig WHERE WorkflowActivityStepFormConfigId=?WorkflowActivityStepFormConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepFormConfigId", workflowActivityStepFormConfigId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流活动步骤表单配置
		public async Task<WorkflowActivityStepFormConfigEntity> GetEntity(long workflowActivityStepFormConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowActivityStepFormConfigId=?WorkflowActivityStepFormConfigId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepFormConfigId", workflowActivityStepFormConfigId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityStepFormConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流活动步骤表单配置列表
		public async Task<IEnumerable<WorkflowActivityStepFormConfigEntity>> GetList(long workflowActivityId)
		{
			string sql = string.Format("{0} WHERE WorkflowActivityId=?WorkflowActivityId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityId", workflowActivityId, DbType.Int64, size: 20);
			return await this.databaseContext.Slave.QueryAsync<WorkflowActivityStepFormConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流活动步骤表单配置分页
		public async Task<(IEnumerable<WorkflowActivityStepFormConfigEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowactivitystepformconfig {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowActivityStepFormConfigEntity, int>(sql, parameters);
		}
		#endregion
	}
}



